﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ISIM
{
    public partial class IncommingTransferDialog : Form
    {
        private ISIMContact contact;
        private SkypeKit.SktConversation conversation;
        private SkypeKit.SktTransfer.List files;

        private string filesList = "";

        public IncommingTransferDialog(ISIMContact contact, SkypeKit.SktTransfer.List files)
        {
            InitializeComponent();

            saveButton.Text = Global.LangManager.Translate("save");
            cancelButton.Text = Global.LangManager.Translate("cancel");

            this.contact = contact;
            this.conversation = (contact.contactObject as SkypeContact.ContactObject).conversation;
            this.files = files;

            this.Text = Global.LangManager.Translate("incommingFiles") + ": " + Global.Converter.GetRenamedContactName(contact);

            for (int i = 0; i < files.Count; i++)
                this.filesList += files[i].P_FILENAME + "( " + Global.Converter.FormatSize(int.Parse(files[i].P_FILESIZE)) + " )" + Environment.NewLine;

            this.filesListTextBox.Text = filesList;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < files.Count; i++)
            {
                string transferId = files[i].P_PARTNER_HANDLE + files[i].P_FILENAME + files[i].P_CONVO_ID;

                files[i].Cancel();

                if (Global.OpenForms.fileTransferringDialogList.ContainsKey(transferId))
                    Global.OpenForms.fileTransferringDialogList[transferId].Invoke(new MethodInvoker(() => Global.OpenForms.fileTransferringDialogList[transferId].Close()));
            }

            Global.ISIMFunctions.AddChatMessage(contact, Global.LangManager.Translate("filesTransferCanceled"), DateTime.Now, Global.ISIM);

            this.Close();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            folderDialog.RootFolder = Environment.SpecialFolder.Desktop;

            if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                for (int i = 0; i < files.Count; i++)
                    files[i].Accept(folderDialog.SelectedPath + System.IO.Path.DirectorySeparatorChar + files[i].P_FILENAME);

                Global.ISIMFunctions.AddChatMessage(contact, Global.LangManager.Translate("transferingFiles") + ":" + "\n" + filesList, DateTime.Now, Global.ISIM);

                this.Close();
            }
        }
    }
}
